﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class LoginPage_AgentRegitser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button_more_Click(object sender, EventArgs e)
    {

        Calendar1.Visible = Calendar1.Visible ? false : true;
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        TextBox_birthday.Text = Calendar1.SelectedDate.ToShortDateString();
    }
    protected void Button_regitser_Click(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            return;
        }
        SqlConnection sqlcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ToString());
        sqlcon.Open();

        string sex = "0";
        if (RadioButton_female.Checked == true)
            sex = "1";

        DateTime datetime1;

        if (!DateTime.TryParse(TextBox_birthday.Text, out datetime1))
        {
            Label_birtdday_note.Visible = true;
            return;
        }
        else
        {
            Label_birtdday_note.Visible = false;
        }


        User user = new User(TextBox_IDCard.Text, TextBox_name.Text, sex, TextBox_birthday.Text);

        int userDataStatus = user.compareToData(sqlcon);
        Label_idcard_note.Visible = false;
        if (userDataStatus == 1)
        {
            Label_idcard_note.Text = "用户信息有误，请仔细核对身份信息！";
            Label_idcard_note.Visible = true;
            return;
        }

        Agent agent = new Agent(user, TextBox_account.Text, DropDownList_school.SelectedValue, TextBox_phone.Text, TextBox_psw2.Text);


        if (agent.checkIDCard(sqlcon))
        {
            Label_idcard_note.Text = "用户已经被注册！";
            Label_idcard_note.Visible = true;
            return;
        }

        bool insertOk = false;

        if (TextBox_account.Text.Equals(""))
        {
            Label_account_note.Visible = true;
            Label_account_note.Text = "账户不能为空！";
            sqlcon.Close();
            return;
        }

        if (Agent.isExist(sqlcon, TextBox_account.Text))
        {
            Label_account_note.Visible = true;
            Label_account_note.Text = "账户已经存在！";
            sqlcon.Close();
            return;
        }
        else
        {
            Label_account_note.Visible = true;
            Label_account_note.Text = "账户可用";
        }
        SqlFormat.beginTran(sqlcon);
        if (agent.insert(sqlcon) != null)
        {
            insertOk = true;
        }

       


        if (insertOk)
        {
            SqlFormat.commit(sqlcon);
            Response.Redirect("RegisterOk.aspx");
        }
        else
        {
            SqlFormat.rollback(sqlcon);
        }
        sqlcon.Close();
    }
    protected void TextBox_account_TextChanged(object sender, EventArgs e)
    {

    }
}